#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/vmalloc.h>



static int my_open(struct inode *inode, struct file *file)
{
	printk("hello open \n");
	return 0;
}


static int dev;
static struct file_operations my_fops = {
	.owner = THIS_MODULE,
	.open = my_open,
};
static struct class *my_class;

static int __init hello_init(void)
{
	struct device *hello_dev;
	printk("hello_init\n");
	dev = register_chrdev(0,"hello",&my_fops);
	if(dev <0)
	{
		printk("register_chrdev error\n");
		return dev;
	}
	my_class = class_create(THIS_MODULE,"hello_cl");
	if(IS_ERR(my_class))
	{
		printk("class_create error\n");
		unregister_chrdev(dev,"hello");
		return -1;
	}
	hello_dev = device_create(my_class,NULL,MKDEV(dev,0),NULL,"hello");
	if(IS_ERR(hello_dev))
	{
		printk("device_create error\n");
		unregister_chrdev(dev,"hello");
		class_destroy(my_class);
		return -1;
	}
	return 0;
}

static void __exit hello_exit(void)
{
	device_destroy(my_class,MKDEV(dev,0));
	class_destroy(my_class);
	unregister_chrdev(dev,"hello");
	printk("hello_exit\n");
}


module_init(hello_init)
module_exit(hello_exit)
MODULE_LICENSE("GPL");
